$(document).ready(function () {
	// 侧边固定栏高度计算
	var sideHeight = $('#sideNav').offset().top;
	sideHeight = sideHeight - 110;
	$('#sideNavList').affix({
	  offset: {
	    top: sideHeight
	  }
	});
	
	// 无穷滚动事件
	var page = 0;
	var timer = 0;
	function checkScrollPosition() {
		var distance = $(window).scrollTop() + $(window).height();
		if(($('#kindsOfNews').height() + $('#roll').height() + $('#hotNews').height()) <= distance) {
			$(document).trigger('nextPage');
			page+=4;
		}
	}
	$(document).on('nextPage', function (event) {
		var data = {
			page: page
		}
		$.post('/nextPage', data, function (result) {
			if(result.status == 1) {
				result.data.forEach(function (item) {
					var html = "";
					html += '<li class="list-group-item"><div class="row"><div class="col-xs-4 col-md-2"><a href="/news?newsId='+ item.id +'"><img src="'+ item.smallPicture +'" alt="Hot news" class="img-responsive img-thumbnail" /></a></div>'
							+ '<div class="col-xs-8 col-md-10"><a href="/news?newsId='+ item.id +'"><h4>'+ item.title +'<small>'+ item.smallTitle +'</small></h4></a><p>'+ item.summary +'</p></div></div>'
							+ '<div class="row text-right"><div class="visible-md-block visible-lg-block col-md-3 col-md-offset-2"><span>—— '+ item.name +' </span></div>'
							+ '<div class="col-md-3"><time>'+ item.sendTime +'</time></div><div class="col-md-4"><span class="span-info">点击量 <span class="badge">'+ item.click +'</span></span>'
							+ '<span>评论数 <span class="badge">'+ item.comment +'</span></span></div></div></li>';
					$('ul#nextPage').append(html);
				});
			}
		});
	});
	$(window).scroll(function () {
		if (!timer) {
			timer = setTimeout(function () {
				checkScrollPosition();
				timer = 0;
			}, 250);
		}
	}).trigger('scroll');
});
